package com.shelpe.services.sinbad.repository.jpa;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.shelpe.services.sinbad.entity.jpa.PurchaseOrder;

public interface PurchaseOrderRepository
		extends PagingAndSortingRepository<PurchaseOrder, Long>, JpaSpecificationExecutor<PurchaseOrder> {

	List<PurchaseOrder> findByNickOrderByOrderCycleStartDesc(String nick);

	List<PurchaseOrder> findByNickIn(List<String> nicks);

	@Transactional
	@Modifying
	@Query(value = "select * from purchase_users_order a where order_cycle_start in(select max(order_cycle_start) from purchase_users_order b where a.nick=b.nick group by nick) and nick IN :nicks", nativeQuery = true)
	List<PurchaseOrder> findNickByPurchaseTime(@Param("nicks") List<String> nicks);
	
	@Transactional
	@Modifying
	@Query(value = "select * from purchase_users_order a where order_cycle_start in(select min(order_cycle_start) from purchase_users_order b where a.nick=b.nick group by nick) and nick IN :nicks", nativeQuery = true)
	List<PurchaseOrder> findNickByMinPurchaseTime(@Param("nicks") List<String> nicks);
}
